package com.qm.console.service.impl;

import com.qm.console.dao.dm.SysUserRoleMapper;
import com.qm.console.dao.dm.UserInfoMapper;
import com.qm.console.model.UserInfo;
import com.qm.console.service.UserInfoService;
import com.qm.console.service.UserInfoService;

import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Edison.hu on 18/7/25.
 */
@Service("UserInfoService")
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    UserInfoMapper userInfoMapper;

    @Autowired
    SysUserRoleMapper sysUserRoleMapper;

    @Override
    public UserInfo findByUsername(String username) {
        List <UserInfo> userInfos = userInfoMapper.findByUsername(username);
        if (userInfos.size()==1)
        {
            return userInfos.get(0);
        }
        else {
            return null;
        }
    }

    @Override
    public UserInfo findByUid(int uid) {
        return userInfoMapper.selectByPrimaryKey(uid);
    }

    @Override
    public int addNewUser(String password,String username) {
        SecureRandomNumberGenerator secureRandomNumberGenerator = new SecureRandomNumberGenerator();
        String salt = secureRandomNumberGenerator.nextBytes().toHex();
        Md5Hash md5 = new Md5Hash(password,username+salt,2);
        String newMd5Password = md5.toHex();
        int res = userInfoMapper.insertNewUser(newMd5Password,username,salt);
        return res;
    }

    @Override
    public int delUserByUsername(String username) {
        return userInfoMapper.delUserByUsername(username);
    }

    @Override
    public void delUserByUid(int uid) {
        userInfoMapper.deleteByPrimaryKey(uid);
    }

    @Override
    public List<UserInfo> getUserInfoList(int offset, int pageSize, String username, String startTime, String endTime,
                                          String orderBy, String orderType) {
        return userInfoMapper.getUserInfoList(offset, pageSize, username, startTime, endTime, orderBy, orderType);
    }

    @Override
    public int getUserInfoCount(String username, String startTime, String endTime) {
        return userInfoMapper.getUserInfoCount(username, startTime, endTime);
    }

    @Override
    public int resetPassword(UserInfo userInfo, String password) {
        SecureRandomNumberGenerator secureRandomNumberGenerator = new SecureRandomNumberGenerator();
        //String salt = secureRandomNumberGenerator.nextBytes().toHex();
        Md5Hash md5 = new Md5Hash(password,userInfo.getUsername()+userInfo.getSalt(),2);
        String newMd5Password = md5.toHex();
        userInfo.setPassword(newMd5Password);
        int ret = userInfoMapper.updateByPrimaryKey(userInfo);
        return ret;
    }

    @Override
    public Boolean UpdatePassword(UserInfo userInfo, String oldPassword, String newPassword) {
        SecureRandomNumberGenerator secureRandomNumberGenerator = new SecureRandomNumberGenerator();
        //String salt = secureRandomNumberGenerator.nextBytes().toHex();
        Md5Hash md5 = new Md5Hash(oldPassword,userInfo.getUsername()+userInfo.getSalt(),2);
        String oldPasswordBySalt = md5.toHex();
        if (oldPasswordBySalt.equals(userInfo.getPassword()))
        {
            Md5Hash newMd5 = new Md5Hash(newPassword,userInfo.getUsername()+userInfo.getSalt(),2);
            String newMd5Password = newMd5.toHex();
            userInfo.setPassword(newMd5Password);
            userInfoMapper.updateByPrimaryKey(userInfo);
            return true;
        }
        else
        {
            return false;
        }

    }


}

